//
//  YDHDateMonthPicker.m
//  YunDaHome-iOS
//
//  Created by yunda on 2020/8/10.
//  Copyright © 2020 YunDa. All rights reserved.
//

#import "YDHDateMonthPicker.h"

#define DateMINYEAR 1900
#define DateMAXYEAR 2099

@interface YDHDateMonthPicker()<UIPickerViewDataSource, UIPickerViewDelegate>

@property (strong, nonatomic) UILabel *titleLabel;

@property (strong, nonatomic) UIPickerView *datePicker;

@property (strong, nonatomic)  UIButton *cancleButton;

@property (strong, nonatomic)  UIButton *okButton;

@property (strong, nonatomic)  UIView *lineView;

// 年数组
@property (strong, nonatomic)  NSMutableArray *yearArray;
// 月数组
@property (strong, nonatomic)  NSMutableArray *monthArray;
// 当前选择的年
@property (strong, nonatomic)  NSString *currentYear;
// 当前选择的月
@property (strong, nonatomic)  NSString *currentMonth;

@end

@implementation YDHDateMonthPicker

#pragma mark--THE LIFE CYCLE
- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (!self) {
        return nil;
    }
    self.frame = CGRectMake(0, 0, J_SCREEN_WIDTH, J_SCREEN_HEIGHT);
    self.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:.1];
    
    [self addSubview:self.viewBg];
    [self.viewBg addSubview:self.titleLabel];
    [self.viewBg addSubview:self.cancleButton];
    [self.viewBg addSubview:self.okButton];
    [self.viewBg addSubview:self.lineView];
    [self.viewBg addSubview:self.datePicker];
    CGFloat h = _datePicker.v_MaxY+J_Height(20);
    self.viewBg.frame = CGRectMake(0, J_SCREEN_HEIGHT-h, self.frame.size.width, h);
    
   self.animationType = PopAnimationType_Bottom;
    
    self.defaultDate = [NSDate date];
        
    return self;
}

- (void)cancleAction {
    [self dismiss];
}

- (void)okAction {
    if (self.selectedDateBlock) {
        NSString *string = [NSString stringWithFormat:@"%@%@",self.currentYear,self.currentMonth];
        NSDate *date = [NSDate j_dateWithString:string format:@"yyyyMM"];
        self.selectedDateBlock(date);
    };
    [self dismiss];
}

#pragma mark--UIPickerViewDelegate
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
    return 2;
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
    if (component == 0) {
        return self.yearArray.count;
    }else{
        return self.monthArray.count;
    }
}

- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component{
    return 34;
}

- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view {
    UILabel *customLabel = (UILabel *)view;
    if (!customLabel) {
        customLabel = [[UILabel alloc] init];
        customLabel.textAlignment = NSTextAlignmentCenter;
        [customLabel setBackgroundColor:[UIColor clearColor]];
        customLabel.textColor = [UIColor blackColor];
        customLabel.font = [UIFont systemFontOfSize:24];
        customLabel.adjustsFontSizeToFitWidth = YES;
    }
    if (component == 0) {
        NSString *title = self.yearArray[row];
        customLabel.text = [NSString stringWithFormat:@"%@%@",title,FYLocalizedString(@"pelvic_floor_year", @"")];
    }else{
        NSString *title = self.monthArray[row];
        customLabel.text = [NSString stringWithFormat:@"%@%@",title,FYLocalizedString(@"pelvic_floor_month", @"")];
    }
    return customLabel;
}


- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
    if (component == 0) {
        self.currentYear = self.yearArray[row];
    }else if (component == 1){
        self.currentMonth = self.monthArray[row];
    }
}

#pragma mark--SETTER
- (void)setDefaultDate:(NSDate *)defaultDate{
    _defaultDate = defaultDate;
    self.currentYear = [NSString stringWithFormat:@"%@",@(defaultDate.j_year)];
    self.currentMonth = [NSString stringWithFormat:@"%02ld",(long)defaultDate.j_month];
    NSInteger yearIndex = [self.yearArray indexOfObject:self.currentYear];
    NSInteger monthIndex = [self.monthArray indexOfObject:self.currentMonth];
    [_datePicker selectRow:yearIndex inComponent:0 animated:YES];
    [_datePicker selectRow:monthIndex inComponent:1 animated:YES];
}

#pragma mark--GETTER
- (UILabel *)titleLabel{
    if (!_titleLabel) {
        CGFloat w = J_Width(150);
        _titleLabel = [[UILabel alloc]initWithFrame:CGRectMake((J_SCREEN_WIDTH-w)/2, 16, w, 30)];
        _titleLabel.textAlignment = NSTextAlignmentCenter;
        _titleLabel.textColor = J_ColorFromHEX(0x000000);
        _titleLabel.font = J_Font(17);
        _titleLabel.text = FYLocalizedString(@"app__select_date", @"");
    }
    return _titleLabel;
}

- (UIButton *)cancleButton{
    if (!_cancleButton) {
        _cancleButton = [UIButton buttonWithType:UIButtonTypeCustom];
        _cancleButton.frame = CGRectMake(J_Width(16), _titleLabel.v_y, J_Width(100), _titleLabel.v_h);
        [_cancleButton setTitle:FYLocalizedString(@"oxygen_top_cancel", @"") forState:UIControlStateNormal];
        [_cancleButton setTitleColor:J_ColorFromHEX(0x999999) forState:UIControlStateNormal];
        _cancleButton.titleLabel.font = J_Font(15);
        [_cancleButton addTarget:self action:@selector(cancleAction) forControlEvents:UIControlEventTouchUpInside];
    }
    return _cancleButton;
}

- (UIButton *)okButton{
    if (!_okButton) {
        _okButton = [UIButton buttonWithType:UIButtonTypeCustom];
        _okButton.frame = CGRectMake(J_SCREEN_WIDTH-_cancleButton.v_w-J_Width(16), _titleLabel.v_y, _cancleButton.v_w, _titleLabel.v_h);
        [_okButton setTitle:FYLocalizedString(@"oxygen_top_determine", @"") forState:UIControlStateNormal];
        [_okButton setTitleColor:J_ColorFromHEX(0x3C96FF) forState:UIControlStateNormal];
        _okButton.titleLabel.font = J_Font(15);
        [_okButton addTarget:self action:@selector(okAction) forControlEvents:UIControlEventTouchUpInside];
    }
    return _okButton;
}

- (UIView *)lineView{
    if (!_lineView) {
        _lineView = [[UIView alloc]initWithFrame:CGRectMake(0, _titleLabel.v_MaxY+16, J_SCREEN_WIDTH, 0.5)];
        _lineView.backgroundColor = J_ColorFromHEX(0xE5E5E5);
    }
    return _lineView;
}

- (UIPickerView *)datePicker{
    if (!_datePicker) {
        _datePicker = [[UIPickerView alloc]initWithFrame:CGRectMake(J_Width(10), _lineView.v_MaxY+20, J_SCREEN_WIDTH-J_Width(10)*2, J_Height(140))];
        _datePicker.delegate = self;
        _datePicker.dataSource = self;
    }
    return _datePicker;
}

- (NSMutableArray *)yearArray{
    if (!_yearArray) {
        _yearArray = [NSMutableArray array];
        for (NSInteger i = DateMINYEAR; i<= DateMAXYEAR; i++) {
            NSString *num = [NSString stringWithFormat:@"%ld",(long)i];
            [_yearArray addObject:num];
        }
    }
    return _yearArray;
}

- (NSMutableArray *)monthArray{
    if (!_monthArray) {
        _monthArray = [NSMutableArray array];
        for (int i = 1; i <= 12; i++) {
            NSString *num = [NSString stringWithFormat:@"%02d",i];
            [_monthArray addObject:num];
        }
    }
    return _monthArray;
}

@end

